<?php

namespace app\datacenter\model;

use app\BaseModel;
use think\facade\Db;
use think\model\relation\HasOne;

/**
 * 设备模型'c.name as city_name'
 */
class Machine extends BaseModel
{
    protected $connection = 'iot';

    /**
     * 获取商户关联设备信息
     * @return HasOne
     */
    public function shop(): HasOne
    {
        return $this->hasOne(Shop::class, 'id', 'shop')->field(['lgt_and_lat', 'id']);
    }

    /**
     * 获取设备位置信息
     * @param string $mac_id
     * @return mixed
     */
    public static function getMachineLocation(string $mac_id): mixed
    {
        return Db::connect('iot')
            ->table('machine')
            ->alias('m')
            ->join('areas p', 'p.id = m.province')
            ->join('areas c', 'c.id = m.city')
            ->where('m.mac_id', $mac_id)
            ->column('m.*,c.name as city_name,p.name as province_name');
    }

    /**
     * 获取设备位置信息
     * @param string $ime
     * @return mixed
     */
    public static function getMacId($ime)
    {
        return Db::connect('iot')
            ->table('machine_stock')
            ->alias('m')
            ->where('m.secret',$ime)
            ->find();
//            ->column('m.*');
    }


}